home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cujoct93.zip / 1110090A < prev    next >
Text File  |  1993-08-10  |  1KB  |  73 lines

  1. /* stack2.c */
  2.  
  3. #include "stack2.h"
  4.  
  5. #define MAX_STACKS  16
  6. #define STACK_SIZE 100
  7.  
  8. static struct stack
  9. {
  10.     int data[STACK_SIZE];
  11.     int ptr;
  12.     int active;
  13. } stacks[MAX_STACKS];
  14.  
  15. int stack_open(void)
  16. {
  17.     int i;
  18.  
  19.     /* Activate an inactive stack */
  20.     for (i = 0; i < MAX_STACKS; ++i)
  21.         if (!stacks[i].active)
  22.         {
  23.             stacks[i].ptr = 0;
  24.             stacks[i].active = 1;
  25.             return i;
  26.         }
  27.  
  28.     return STACK_ERROR;
  29. }
  30.  
  31. int stack_push(int id, int x)
  32. {
  33.     if (id >= 0 && id < MAX_STACKS && stacks[id].active)
  34.     {
  35.         int ptr = stacks[id].ptr;
  36.  
  37.         if (ptr == STACK_SIZE)
  38.             return STACK_ERROR;
  39.         else
  40.         {
  41.             stacks[id].data[ptr] = x;
  42.             ++stacks[id].ptr;
  43.             return x;
  44.         }
  45.     }
  46.     else
  47.         return STACK_ERROR;
  48. }
  49.  
  50. int stack_pop(int id)
  51. {
  52.     if (id >= 0 && id < MAX_STACKS && stacks[id].active)
  53.     {
  54.         int ptr = stacks[id].ptr;
  55.  
  56.         if (ptr == 0)
  57.             return STACK_ERROR;
  58.         else
  59.         {
  60.             ptr = --stacks[id].ptr;
  61.             return stacks[id].data[ptr];
  62.         }
  63.     }
  64.     else
  65.         return STACK_ERROR;
  66. }
  67.  
  68. void stack_close(int id)
  69. {
  70.     if (id >= 0 && id < MAX_STACKS)
  71.         stacks[id].active = 0;
  72. }
  73.